﻿<%@ WebHandler Language="C#" Class="FleetHandler" %>

using System;
using System.Web;
using System.IO;
using System.Data;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

using CE.Database;
using CE.InputOut;

public class FleetHandler : IHttpHandler 
{
    
    public void ProcessRequest (HttpContext context) 
    {
        context.Response.Clear();

        if (context.Request["fileName"] != null && context.Request["carId"] != null && context.Request["userId"] != null && context.Request["providerId"] != null)
        {
            string _filePath = "";
            string _file = context.Request["fileName"].ToString();
            string _carId = context.Request["carId"].ToString();
            string _userId = context.Request["userId"].ToString();
            string _providerId = context.Request["providerId"].ToString();

            MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
            string _spName = "sprocCXCRMFleetCarFileUploadDetailSelectItemList";
            SortedList _spara = new SortedList();
            _spara.Add("@FleetCarId", Convert.ToInt64(_carId.ToString()));
            _spara.Add("@ProviderCompanyId", Convert.ToInt64(_providerId.ToString()));
            DataSet _dsCarDocumentDetails = new DataSet();
            _dsCarDocumentDetails = _db.GetDataSet(_spName, _spara);
            if (_file.ToString() == "test.pdf")
            {
                Int16 fileCount = 0;
                if (context.Request["fileCount"] != null)
                {
                    fileCount = Convert.ToInt16(context.Request.QueryString["fileCount"].ToString());
                }
                if (_dsCarDocumentDetails != null && _dsCarDocumentDetails.Tables.Count > 1)
                {
                    if (_dsCarDocumentDetails.Tables[1].Rows.Count > 0)
                    {
                        _filePath = _dsCarDocumentDetails.Tables[1].Rows[fileCount]["FileName"].ToString();
                        string fileName = context.Request.QueryString["fileName"].ToString();

                        string filePath = _filePath;
                        FileInfo file = new System.IO.FileInfo(filePath);
                        if (!file.Exists)
                        {
                            context.Response.StatusCode = 500;
                            context.Response.End();
                            return;
                        }
                        context.Response.Clear();
                        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        context.Response.Cache.SetNoStore();
                        context.Response.Cache.SetExpires(DateTime.MinValue);
                        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                        context.Response.AddHeader("Content-Length", file.Length.ToString());
                        context.Response.ContentType = "application/octet-stream";
                        context.Response.WriteFile(file.FullName);
                        context.ApplicationInstance.CompleteRequest();
                        context.Response.End();
                    }
                }
            }
            if (_file.ToString() == "test.xls")
            {
                if (_dsCarDocumentDetails != null && _dsCarDocumentDetails.Tables.Count > 2)
                {
                    if (_dsCarDocumentDetails.Tables[2].Rows.Count > 0)
                    {
                        _filePath = _dsCarDocumentDetails.Tables[2].Rows[0]["FileName"].ToString();
                        string fileName = context.Request.QueryString["fileName"].ToString();

                        string filePath = _filePath;
                        FileInfo file = new System.IO.FileInfo(filePath);
                        if (!file.Exists)
                        {
                            context.Response.StatusCode = 500;
                            context.Response.End();
                            return;
                        }
                        context.Response.Clear();
                        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        context.Response.Cache.SetNoStore();
                        context.Response.Cache.SetExpires(DateTime.MinValue);
                        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                        context.Response.AddHeader("Content-Length", file.Length.ToString());
                        context.Response.ContentType = "application/octet-stream";
                        context.Response.WriteFile(file.FullName);
                        context.ApplicationInstance.CompleteRequest();
                        context.Response.End();
                    }
                }
            }
        }
    }
 
    public bool IsReusable 
    {
        get 
        {
            return false;
        }
    }

}